﻿namespace AnyOffice.WebControls
{
    using System;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ToolboxData("<{0}:HFTextBox runat=server></{0}:HFTextBox>"), DefaultProperty("Text")]
    public class HFTextBox : TextBox
    {
        protected FieldTypeMode _FieldTypeMode;
        private NotNUllType _NotNUllType = NotNUllType.False;
        private string strChineseName = "";
        private string strFieldLength = "";
        private string strPropertyName = "";
        private string strPropertyValue = "";

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.CssClass = "dibg";
            this.FieldType = this.FieldType;
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(this.Text);
        }

        private void TypeValidate()
        {
            switch (this._FieldTypeMode)
            {
                case FieldTypeMode.字符串:
                    this.Text = "";
                    break;

                case FieldTypeMode.整型:
                    this.Text = "0";
                    break;

                case FieldTypeMode.浮点型:
                    this.Text = "0.00";
                    break;

                case FieldTypeMode.日期:
                    this.Text = DateTime.Now.ToString("yyyy-mm-dd");
                    break;

                case FieldTypeMode.日期时间:
                    this.Text = DateTime.Now.ToString("yyyy-mm-dd hh:ss");
                    break;

                case FieldTypeMode.时间:
                    this.Text = DateTime.Now.ToString("hh:ss");
                    break;
            }
        }

        [Category("HFServer"), Browsable(true), Description("属性对应的中文名，目的是在数据校验的时候抛出异常提示时使用"), Bindable(true)]
        public string ChineseName
        {
            get
            {
                if (base.Attributes["ChineseName"] != null)
                {
                    this.strChineseName = base.Attributes["ChineseName"].Trim();
                }
                return this.strChineseName;
            }
            set
            {
                this.strChineseName = value;
                base.Attributes.Add("ChineseName", this.strChineseName);
            }
        }

        [DefaultValue(0), Bindable(true), Description("控件对应数据库字段的长度"), Browsable(true), Category("HFClient")]
        public string FieldLength
        {
            get
            {
                if (base.Attributes["FieldLength"] != null)
                {
                    this.strFieldLength = base.Attributes["FieldLength"].Trim();
                }
                return this.strFieldLength;
            }
            set
            {
                this.strFieldLength = value;
                base.Attributes.Add("FieldLength", this.strFieldLength);
            }
        }

        [Description("控件对应的数据库字段类型，目的是进行客户端校验"), Bindable(true), Category("HFClient"), Browsable(true), DefaultValue("字符串")]
        public FieldTypeMode FieldType
        {
            get
            {
                return this._FieldTypeMode;
            }
            set
            {
                this._FieldTypeMode = value;
                base.Attributes.Add("FieldType", this._FieldTypeMode.ToString());
                this.TypeValidate();
            }
        }

        [Description("选择True，表示该控件对应的数据库字段非空"), Bindable(true), DefaultValue("False"), Category("HFClient"), Browsable(true)]
        public NotNUllType NotNUll
        {
            get
            {
                return this._NotNUllType;
            }
            set
            {
                this._NotNUllType = value;
                base.Attributes.Add("NotNUll", this._NotNUllType.ToString());
            }
        }

        [Browsable(true), Description("对应实体类属性的名称"), Bindable(true), Category("HFServer")]
        public string PropertyName
        {
            get
            {
                if (base.Attributes["PropertyName"] != null)
                {
                    this.strPropertyName = base.Attributes["PropertyName"].Trim();
                }
                return this.strPropertyName;
            }
            set
            {
                this.strPropertyName = value;
                base.Attributes.Add("PropertyName", this.strPropertyName);
            }
        }

        [Browsable(true), Bindable(true), Category("HFServer"), Description("保存控件对应数据库字段当前行的原值")]
        public string PropertyValue
        {
            get
            {
                if (base.Attributes["PropertyValue"] != null)
                {
                    this.strPropertyValue = base.Attributes["PropertyValue"];
                }
                return this.strPropertyValue;
            }
            set
            {
                this.strPropertyValue = value;
                base.Attributes.Add("PropertyValue", this.strPropertyValue);
            }
        }

        public enum FieldTypeMode
        {
            字符串,
            整型,
            浮点型,
            日期,
            日期时间,
            时间
        }

        public enum NotNUllType
        {
            False,
            True
        }
    }
}

